<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司代码" prop="gcdm">
        <el-input
          v-model="queryParams.gcdm"
          placeholder="请输入公司代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备位置" prop="sbwz">
        <el-input
          v-model="queryParams.sbwz"
          placeholder="请输入设备位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生时间" prop="fssj">
        <el-date-picker clearable
          v-model="queryParams.fssj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否停机" prop="sftj">
        <el-input
          v-model="queryParams.sftj"
          placeholder="请输入是否停机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障描述" prop="gzms">
        <el-input
          v-model="queryParams.gzms"
          placeholder="请输入故障描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障图片" prop="gztp">
        <el-input
          v-model="queryParams.gztp"
          placeholder="请输入故障图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修语音" prop="bxyy">
        <el-input
          v-model="queryParams.bxyy"
          placeholder="请输入报修语音"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修班组" prop="wxbz">
        <el-input
          v-model="queryParams.wxbz"
          placeholder="请输入维修班组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急程度" prop="jjcd">
        <el-input
          v-model="queryParams.jjcd"
          placeholder="请输入紧急程度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障等级" prop="gzdj">
        <el-input
          v-model="queryParams.gzdj"
          placeholder="请输入故障等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障类别" prop="gzlb">
        <el-input
          v-model="queryParams.gzlb"
          placeholder="请输入故障类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="gcmc">
        <el-input
          v-model="queryParams.gcmc"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属组织" prop="sszz">
        <el-input
          v-model="queryParams.sszz"
          placeholder="请输入所属组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理进度" prop="cljd">
        <el-input
          v-model="queryParams.cljd"
          placeholder="请输入处理进度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主修人" prop="zxr">
        <el-input
          v-model="queryParams.zxr"
          placeholder="请输入主修人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主修人id" prop="zxrid">
        <el-input
          v-model="queryParams.zxrid"
          placeholder="请输入主修人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修单号" prop="bxdh">
        <el-input
          v-model="queryParams.bxdh"
          placeholder="请输入报修单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="sbbm">
        <el-input
          v-model="queryParams.sbbm"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="sbmc">
        <el-input
          v-model="queryParams.sbmc"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="ggxh">
        <el-input
          v-model="queryParams.ggxh"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用部门" prop="sybm">
        <el-input
          v-model="queryParams.sybm"
          placeholder="请输入使用部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修人" prop="bxr">
        <el-input
          v-model="queryParams.bxr"
          placeholder="请输入报修人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="lxfs">
        <el-input
          v-model="queryParams.lxfs"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:gzbx:add']"
        >新增报修单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:gzbx:edit']"
        >编辑报修单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:gzbx:remove']"
        >取消报修单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-custom"
          size="mini"
          @click="handleJobSub"
          :disabled="single"
          v-hasPermi="['device:gzbx:jobSub']"
        >指派维修员</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:gzbx:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :row-class-name="tableAddClass" :data="gzbxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="公司代码" align="center" prop="gcdm" />
      <el-table-column label="设备位置" align="center" prop="sbwz" />
      <el-table-column label="发生时间" align="center" prop="fssj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fssj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否停机" align="center" prop="sftj" />
      <el-table-column label="故障描述" align="center" prop="gzms" />
      <el-table-column label="故障图片" align="center" prop="gztp" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.gztp" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="报修语音" align="center" prop="bxyy" >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-video-play"
            @click="handlePlayMp3(scope.row)"
          >播放</el-button>
        </template>
      </el-table-column>
      <el-table-column label="维修班组" align="center" prop="wxbz" />
      <el-table-column label="紧急程度" align="center" prop="jjcd" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_fault_urgency" :value="scope.row.jjcd"/>
        </template>
      </el-table-column>
      <el-table-column label="故障等级" align="center" prop="gzdj" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_fault_level" :value="scope.row.gzdj"/>
        </template>
      </el-table-column>
      <el-table-column label="故障类别" align="center" prop="gzlb" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_fault_type" :value="scope.row.faultType"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="gcmc" />
      <el-table-column label="所属组织" align="center" prop="sszz" />
      <el-table-column label="处理进度" align="center" prop="cljd" >
        <template slot-scope="scope">
          <!--   <el-badge :value="1" :max="1" :hidden="isHidden" :type="badgeType"  class="item">
             </el-badge>-->
          <dict-tag :options="dict.type.device_fault_progress" :value="scope.row.cljd"/>
        </template>
      </el-table-column>
      <el-table-column label="主修人" align="center" prop="zxr" />
      <el-table-column label="主修人id" align="center" prop="zxrid" />
      <el-table-column label="报修单号" align="center" prop="bxdh" />
      <el-table-column label="设备编码" align="center" prop="sbbm" />
      <el-table-column label="设备名称" align="center" prop="sbmc" />
      <el-table-column label="规格型号" align="center" prop="ggxh" />
      <el-table-column label="使用部门" align="center" prop="sybm" />
      <el-table-column label="报修人" align="center" prop="bxr" />
      <el-table-column label="联系方式" align="center" prop="lxfs" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:gzbx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:gzbx:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备故障报修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修单号" prop="bxdh">
          <el-input v-model="form.bxdh" placeholder="请输入报修单号" />
        </el-form-item>
        <el-form-item label="设备编码" prop="sbbm">
          <el-input v-model="form.sbbm" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备名称" prop="sbmc">
          <el-input v-model="form.sbmc" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="ggxh">
          <el-input v-model="form.ggxh" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="使用部门" prop="sybm">
          <el-input v-model="form.sybm" placeholder="请输入使用部门" />
        </el-form-item>
        <el-form-item label="报修人" prop="bxr">
          <el-input v-model="form.bxr" placeholder="请输入报修人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="lxfs">
          <el-input v-model="form.lxfs" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="设备位置" prop="sbwz">
          <el-input v-model="form.sbwz" placeholder="请输入设备位置" />
        </el-form-item>
        <el-form-item label="发生时间" prop="fssj">
          <el-date-picker clearable
                          v-model="form.fssj"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="故障描述" prop="gzms">
          <el-input v-model="form.gzms" placeholder="请输入故障描述" />
        </el-form-item>

        <el-form-item label="故障图片" v-show="isFaultImg">
          <image-upload   v-model="form.gztp"/>
        </el-form-item>

        <el-form-item label="紧急程度" prop="jjcd">
          <el-select v-model="form.jjcd" placeholder="请选择紧急程度">
            <el-option
              v-for="dict in dict.type.device_fault_urgency"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障等级" prop="gzdj">
          <el-select v-model="form.gzdj" placeholder="请选择故障等级" >
            <el-option
              v-for="dict in dict.type.device_fault_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障类别" prop="gzlb">
          <el-select v-model="form.gzlb" placeholder="请选择故障类别">
            <el-option
              v-for="dict in dict.type.device_fault_type"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="公司代码" prop="gcdm">
          <el-input v-model="form.gcdm" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="是否停机" prop="sftj">
          <el-input v-model="form.sftj" placeholder="请输入是否停机" />
        </el-form-item>
        <el-form-item label="报修语音" prop="bxyy">
          <el-input v-model="form.bxyy" placeholder="请输入报修语音" />
        </el-form-item>
        <el-form-item label="维修班组" prop="wxbz">
          <el-input v-model="form.wxbz" placeholder="请输入维修班组" />
        </el-form-item>
        <el-form-item label="公司名称" prop="gcmc">
          <el-input v-model="form.gcmc" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="所属组织" prop="sszz">
          <el-input v-model="form.sszz" placeholder="请输入所属组织" />
        </el-form-item>
        <el-form-item label="处理进度" prop="cljd">
          <el-input v-model="form.cljd" placeholder="请输入处理进度" />
        </el-form-item>
        <el-form-item label="主修人" prop="zxr">
          <el-input v-model="form.zxr" placeholder="请输入主修人" />
        </el-form-item>
        <el-form-item label="主修人id" prop="zxrid">
          <el-input v-model="form.zxrid" placeholder="请输入主修人id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--派工-->
    <el-dialog :title="jobSub.title" :visible.sync="jobSub.open" width="1000px" append-to-body>
      <div id="userPost">
        <!-- 注册名 -->
        <userPost ref="userPost"></userPost>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClick">派  工</el-button>
        <el-button @click="jobSub.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGzbx, getGzbx, delGzbx, addGzbx, updateGzbx } from "@/api/device/gzbx";
import userPost from "@/views/system/user/userPost";

export default {
  name: "Gzbx",
  // 注册组件
  components: {
    userPost
  },
  dicts: ['device_fault_progress','device_fault_urgency', 'device_fault_level','device_is_shut_down', 'device_fault_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],

      faultProgresss:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障图片是否上传
      isFaultImg:false,
      // 设备故障报修表格数据
      gzbxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      /*派工界面*/
      jobSub:{
        // 是否显示弹出层（
        open: false,
        // 弹出层标题
        title: "",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gcdm: null,
        sbwz: null,
        fssj: null,
        sftj: null,
        gzms: null,
        gztp: null,
        bxyy: null,
        wxbz: null,
        jjcd: null,
        gzdj: null,
        gzlb: null,
        gcmc: null,
        sszz: null,
        cljd: null,
        zxr: null,
        zxrid: null,
        bxdh: null,
        sbbm: null,
        sbmc: null,
        ggxh: null,
        sybm: null,
        bxr: null,
        lxfs: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sbbm: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        bxr: [
          { required: true, message: "报修人不能为空", trigger: "blur" }
        ],
        sbmc: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableAddClass({row,rowIndex}) {
      if (row.cljd == 0) {
        /* this.isHidden=false;
         this.badgeType="danger";*/
        return "tr-red1";
      }else if(row.cljd == 1){
        /*  this.isHidden=false;
          this.badgeType="warning";*/
        return "tr-red2";
      }else if(row.cljd == 2){
        /*this.isHidden=false;
        this.badgeType="primary";*/
        return "tr-red3";
      }
      return '';
    },
    /** 查询设备故障报修列表 */
    getList() {
      this.loading = true;
      listGzbx(this.queryParams).then(response => {
        this.gzbxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xh: null,
        gcdm: null,
        sbwz: null,
        fssj: null,
        sftj: null,
        gzms: null,
        gztp: null,
        bxyy: null,
        wxbz: null,
        jjcd: null,
        gzdj: null,
        gzlb: null,
        gcmc: null,
        sszz: null,
        cljd: null,
        zxr: null,
        zxrid: null,
        bxdh: null,
        sbbm: null,
        sbmc: null,
        ggxh: null,
        sybm: null,
        bxr: null,
        lxfs: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.faultProgresss = selection.map(item => item.cljd)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isFaultImg=true,
      this.open = true;
      this.title = "添加设备故障报修";
    },
    handlePlayMp3(row){
      /*
            alert(row.reportVoice)
      */
      this.src =process.env.VUE_APP_BASE_API+row.reportVoice;
      this.play();
    },
    //播放
    play() {
      this.dialogVisible = true;
      this.$refs.audio.play();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.isFaultImg=true
      let isUpdate=this.faultProgresss;
      if(isUpdate=='4'){
        this.$modal.alertWarning("此报修单已完成不能修改！");
        return;
      }
      getGzbx(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备故障报修";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGzbx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGzbx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    handleClick() {
      if(this.$refs.userPost.zxrRadio==null){
        this.$modal.alertWarning("未设置主维修人！");
        return;
      }
      let fzMajorId=this.$refs.userPost.zxrRadio;
      if(this.$refs.userPost.ids.length>0){
        fzMajorId=this.$refs.userPost.ids;
        /*this.$modal.alertWarning("未指派维修人员！");
        return;*/
      }
      if(this.reportIds.length===0){
        this.$modal.alertWarning("未选中数据！");
        return;
      }
      let majorId=this.$refs.userPost.zxrRadio;
      let reportId=this.reportIds;
      this.$modal.confirm('确认指派维修人员？').then(function() {
        return addJobSub(reportId,majorId,fzMajorId);
      }).then(() => {
        this.getList();
        this.jobSub.open = false;
        this.$modal.msgSuccess("指派成功");
      }).catch(() => {});

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let isUpdate=this.faultProgresss;
      if(isUpdate=='1'|| isUpdate=='2' || isUpdate=='3'){
        this.$modal.alertWarning("此报修单已接单操作不能取消！");
        return;
      }
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备故障报修编号为"' + ids + '"的数据项？').then(function() {
        return delGzbx(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /*派工*/
    handleJobSub(){
      let isUpdate=this.faultProgresss;
      if(isUpdate!='0'){
        this.$modal.alertWarning("此报修单已派维修人员！");
        return;
      }
      this.jobSub.title = "指派维修人员";
      this.jobSub.open = true;
      setTimeout(() => {
        this.$refs.userPost.getList();
      }, 5);
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('device/gzbx/export', {
        ...this.queryParams
      }, `gzbx_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
/deep/.el-table .tr-red1 {
  background: rgba(255, 43, 31, 0.42) !important;
}
/deep/.el-table .tr-red2 {
  background: oldlace !important;
}
/deep/.el-table .tr-red3 {
  background: #f0f9eb !important;
}
/deep/.el-table .tr-red5 {
  background-color: rgba(23, 27, 26, 0.11) !important;
}
.item {
  margin-top: 10px;
  margin-right: 40px;
}
</style>

